package com.wuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuye.VO.BaseCommunityVo;
import com.wuye.VO.YiBiaoVo;
import com.wuye.basic.domain.WyBalcony;
import com.wuye.basic.domain.WyCommunity;
import com.wuye.basic.domain.WyHome;
import com.wuye.basic.domain.WyUnit;
import com.wuye.basic.mapper.WyBalconyMapper;
import com.wuye.basic.mapper.WyCommunityMapper;
import com.wuye.basic.mapper.WyHomeMapper;
import com.wuye.basic.mapper.WyUnitMapper;
import com.wuye.charge.domain.WyCost;
import com.wuye.charge.domain.WyCostAllocation;
import com.wuye.charge.domain.vo.WyBalconyunithomeVo;
import com.wuye.charge.domain.vo.WyCommunityVo;
import com.wuye.charge.mapper.WyBalconyunithomeVoMapper;
import com.wuye.charge.mapper.WyCostAllocationMapper;
import com.wuye.charge.mapper.WyCostMapper;
import com.wuye.domain.WyYbMoney;
import com.wuye.domain.WyYbType;
import com.wuye.domain.WyYibiao;
import com.wuye.dto.YibiaoAddDto;
import com.wuye.dto.YibiaoSerchDto;
import com.wuye.mapper.WyYbMoneyMapper;
import com.wuye.mapper.WyYbTypeMapper;
import com.wuye.mapper.WyYibiaoMapper;
import com.wuye.service.IWyYibiaoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * 仪表管理Service业务层处理
 * 
 * @author jack
 * @date 2024-11-22
 */
@Service
public class WyYibiaoServiceImpl extends ServiceImpl<WyYibiaoMapper, WyYibiao> implements IWyYibiaoService
{
    @Autowired
    private WyYibiaoMapper wyYibiaoMapper;
    @Autowired
    private WyCommunityMapper wyCommunityMapper;
    @Autowired
    private WyBalconyMapper wyBalconyMapper;

    @Autowired
    private WyUnitMapper wyUnitMapper;

    @Autowired
    private WyHomeMapper wyHomeMapper;

    @Autowired
    private WyYbTypeMapper wyYbTypeMapper;

    @Autowired
    private WyYbMoneyMapper wyYbMoneyMapper;

    @Autowired
    private WyCostMapper wyCostMapper;

    @Autowired
    private WyCostAllocationMapper wyCostAllocationMapper;

    /**
     * 查询仪表管理
     * 
     * @param ybId 仪表管理主键
     * @return 仪表管理
     */
    @Override
    public WyYibiao selectWyYibiaoByYbId(Long ybId)
    {
        return wyYibiaoMapper.selectById(ybId);
    }

    /**
     * 查询仪表管理列表
     * 
     * @param wyYibiao 仪表管理
     * @return 仪表管理
     */
    @Override
    public List<WyYibiao> selectWyYibiaoList(WyYibiao wyYibiao)
    {
        return wyYibiaoMapper.selectWyYibiaoList(wyYibiao);
    }

    @Override
    public List<YiBiaoVo> selectWyYibiaoList2(YibiaoSerchDto yibiaoSerchDto) {
        QueryWrapper<WyYibiao> wrapper = new QueryWrapper<>();
        if (yibiaoSerchDto.getYbName() !=null && yibiaoSerchDto.getYbName() != ""){
            wrapper.like("yb_name", yibiaoSerchDto.getYbName());
        }
        if (yibiaoSerchDto.getCommunityId() !=null){
            wrapper.eq("community_id", yibiaoSerchDto.getCommunityId());
        }
        List<WyYibiao> wyYibiaos = wyYibiaoMapper.selectList(wrapper);
        ArrayList<YiBiaoVo> yiBiaoVos = new ArrayList<>();
        for (WyYibiao wyYibiao : wyYibiaos){
            YiBiaoVo yiBiaoVo = new YiBiaoVo();
            WyCommunity wyCommunity=wyYibiaoMapper.selectByCommunityid(wyYibiao.getCommunityId());
            WyBalcony wyBalcony =wyYibiaoMapper.selectByBalconyId(wyYibiao.getBalconyId());
            WyUnit wyUnit =wyYibiaoMapper.selectByUnitId(wyYibiao.getUnitId());
            WyHome wyHose=wyYibiaoMapper.selectByHouseId(wyYibiao.getHouseId());
            WyYbType wyYbType=wyYibiaoMapper.selectByTypeId(wyYibiao.getYbTypeId());
            BeanUtils.copyProperties(wyYibiao, yiBiaoVo);
            yiBiaoVo.setWyCommunity(wyCommunity);
            yiBiaoVo.setWyBalcony(wyBalcony);
            yiBiaoVo.setWyUnit(wyUnit);
            yiBiaoVo.setWyHome(wyHose);
            yiBiaoVo.setWyYbType(wyYbType);
            yiBiaoVos.add(yiBiaoVo);
        }
        return yiBiaoVos;
    }

    /**
     * 新增仪表管理
     * 
     * @param yibiaoAddDto 仪表管理
     * @return 结果
     */
    @Override
    public int insertWyYibiao(YibiaoAddDto yibiaoAddDto)
    {


        WyBalconyunithomeVo wyBalconyunithomeVo=wyYibiaoMapper.selectByMidId(yibiaoAddDto.getBalconyunithomeId());
        System.out.println(wyBalconyunithomeVo+"----------------------------");

        //判断是否有相同类型的仪表
        List<WyYibiao> yibiao=wyYibiaoMapper.selectByHomeId(wyBalconyunithomeVo.getHomeId());
        if (yibiao.size()>0){
            for (WyYibiao wyYibiao : yibiao){
                if (wyYibiao.getYbTypeId() == yibiaoAddDto.getYbTypeId()){
                    return -1;
                }
            }
        }

        WyBalcony wyBalcony = wyBalconyMapper.selectById(wyBalconyunithomeVo.getBalconyId());
        WyCommunity wyCommunity = wyCommunityMapper.selectById(wyBalcony.getCommunityId());
        WyYibiao wyYibiao = new WyYibiao();
        wyYibiao.setYbCreateTime(new Date());
        wyYibiao.setYbUpdateTime(new Date());
        wyYibiao.setBalconyunithomeId(yibiaoAddDto.getBalconyunithomeId());
        wyYibiao.setCommunityId(wyCommunity.getCommunityId());
        wyYibiao.setBalconyId(wyBalconyunithomeVo.getBalconyId());
        wyYibiao.setUnitId(wyBalconyunithomeVo.getUnitId());
        wyYibiao.setHouseId(wyBalconyunithomeVo.getHomeId());
        wyYibiao.setYbName(yibiaoAddDto.getYbName());
        wyYibiao.setYbTypeId(yibiaoAddDto.getYbTypeId());
        wyYibiao.setYbReamrk(yibiaoAddDto.getYbReamrk());
        return wyYibiaoMapper.insert(wyYibiao);
    }

    /**
     * 修改仪表管理
     * 
     * @param wyYibiao 仪表管理
     * @return 结果
     */
    @Override
    public int updateWyYibiao(WyYibiao wyYibiao)
    {
//        wyYibiao.setYbUpdateTime(new Date());

        return wyYibiaoMapper.updateById(wyYibiao);
    }

    /**
     * 批量删除仪表管理
     * 
     * @param ybIds 需要删除的仪表管理主键
     * @return 结果
     */
    @Override
    public int deleteWyYibiaoByYbIds(Long[] ybIds)
    {
        return wyYibiaoMapper.deleteBatchIds(Arrays.asList(ybIds));
    }

    /**
     * 删除仪表管理信息
     * 
     * @param ybId 仪表管理主键
     * @return 结果
     */
    @Override
    public int deleteWyYibiaoByYbId(Long ybId)
    {
        return wyYibiaoMapper.deleteById(ybId);
    }

    /**
     *查询所有的小区
     * @return
     */
    @Override
    public List<WyCommunity> seleceAllCommunity() {
        return wyCommunityMapper.selectList(null);
    }

    /**
     * 查询所有类型
     * @return
     */
    @Override
    public List<WyYbType> selectAllType() {
        return wyYbTypeMapper.selectList(null);
    }

    @Override
    public Integer updateStatus(Long id) {
        WyYibiao wyYibiao = wyYibiaoMapper.selectById(id);
        if (wyYibiao.getYbStatus() ==1){
            wyYibiao.setYbStatus(0L);
        }else{
            wyYibiao.setYbStatus(1L);
        }
        return wyYibiaoMapper.updateWyYibiao(wyYibiao);
    }

    @Override
    public BaseCommunityVo selectAllcommunityAndBUH() {
        List<WyCommunity> wyCommunities = wyCommunityMapper.selectList(null);
        List<WyBalcony> wyBalconies = wyBalconyMapper.selectList(null);
        List<WyUnit> wyUnits = wyUnitMapper.selectList(null);
        List<WyHome> wyHomes = wyHomeMapper.selectList(null);

        BaseCommunityVo baseCommunityVo = new BaseCommunityVo();
        baseCommunityVo.setCommunities(wyCommunities);
        baseCommunityVo.setBalconies(wyBalconies);
        baseCommunityVo.setUnits(wyUnits);
        baseCommunityVo.setHomes(wyHomes);
        return baseCommunityVo;
    }

    /**
     * 抄表
     * @param id
     * @return
     */
    @Override
    public int chaobiao(Long id) {
        WyYbMoney ybmoney = new WyYbMoney();
        WyYibiao wyYibiao = wyYibiaoMapper.selectById(id);

        Date ybUpdateTime = wyYibiao.getYbUpdateTime();

        ybmoney.setYbId(id);
        ybmoney.setYbMoyBeginTime(ybUpdateTime);
        ybmoney.setYbMoyEndTime(new Date());
        List<WyYbMoney> wyYbMonies = wyYbMoneyMapper.selectByYbIdLastTime(id);
        BigDecimal beginElec = new BigDecimal(0);
        if (wyYbMonies.size() == 0){
            ybmoney.setYbMoyBeginElectricity(beginElec);
        }else {
            beginElec=wyYbMonies.get(0).getYbMoyEndElectricity();
            ybmoney.setYbMoyBeginElectricity(beginElec);
        }

        int num = new Random().nextInt(200);
        BigDecimal endElec = beginElec.add(new BigDecimal(num));
        ybmoney.setYbMoyEndElectricity(endElec);
        ybmoney.setYbMoyRemark(wyYibiao.getYbReamrk());
        ybmoney.setYbMoyCreateTime(new Date());
        WyYbType wyYbType=wyYbTypeMapper.selectByYbId(wyYibiao.getYbTypeId());
        WyCost wyCost = wyCostMapper.selectWyCostByCostId(wyYbType.getCostId());
        ybmoney.setYbMoyMoney(wyCost.getCostPrice().multiply(new BigDecimal(num)).add(wyCost.getCostMore()));

        wyYibiao.setYbUpdateTime(new Date());
        wyYibiaoMapper.updateById(wyYibiao);
        System.out.println(wyYibiao.getCommunityId()+" ==="+wyYibiao.getBalconyunithomeId()+"----"+wyYbType.getCostId());
//        wyCostAllocationMapper.insertWyCostAllocation(new WyCostAllocation(10,wyYibiao.getCommunityId(),wyYibiao.getBalconyunithomeId(),wyYbType.getCostId(),"0",0))
        wyYibiaoMapper.insertCostAll(wyYibiao.getCommunityId(),wyYibiao.getBalconyunithomeId(),wyYbType.getCostId());
        return wyYbMoneyMapper.insert(ybmoney);
    }


}
